//统一管理项目前部的接口
import requests from "./request";
import * as dateUtil from "@/utils/DateUtil"
import contant from "@/api/constants"
//封装函数:复用
//将来这个函数可以在别的地方使用,需要对外暴露【分别暴露】
//获取游戏资讯详情的数据
export const getGameNewsDetail = gameNewsId =>  requests({ method: 'get', url: `gameNews/${gameNewsId}`});

//获取游戏商品详情的数据
export const getGameGoodsDetail = gameGoodsId => requests({ method: 'get', url: `gameGoods/${gameGoodsId}`});

//登录接口
export const login = (username, password) => {
  return requests({url:`/oauth/token?grant_type=${contant.AUTH_GRANT_TYPE}&client_id=${contant.AUTH_CLIENT_ID}&client_secret=${contant.AUTH_CLIENT_SECRET}&username=${username}&password=${password}`,method:'get'})
};

//刷新token
export const refreshToken = (refreshToken) => requests(
  {url:`/oauth/token?grant_type=${contant.AUTH_REFRESH_TYPE}&client_id=${contant.AUTH_CLIENT_ID}&client_secret=${contant.AUTH_CLIENT_SECRET}&refresh_token=${refreshToken}`,method:'get'
});

//订单操作：取消支付，支付成功
export const payOrder = (orderSn, payStatus) => requests({ method: 'get', url: `order/pay?orderSn=${orderSn}&payStatus=${payStatus}`})

//按照状态查询订单列表
export const getOrderList = (orderStatus) => {
  if (orderStatus == 4) {
    return requests({ method: 'get', url: `order/list`})
  }
  return requests({ method: 'get', url: `order/list?status=${orderStatus}`})
}

//确认已收货
export const orderReceive = (orderSn) => requests({ method: 'get', url: `order/receive?orderSn=${orderSn}`})

//查询日志
export const getOrderLog = (orderSn) => {
  if (orderSn == null || orderSn == '') {
    return requests({ method: 'get', url: `order/log` })
  }
  return requests({ method: 'get', url: `order/log?orderSn=${orderSn}`})
}

//申请退款
export const orderRefund = (orderSubSn) => requests({ method: 'get', url: `order/refund?orderSubSn=${orderSubSn}`})

//修改个人余额
export const getUserAccount = () => requests({ method: 'get', url: `user/account`})

//充值
export const userRecharge = (username, money) => requests({ method: 'put', url: `user/recharge?money=${money}&username=${username}`})

//重置密码
export const userResetPassword = (username) => requests({ method: 'put', url: `user/resetPassword?username=${username}`})

//获取游戏商品列表的数据
export const getGameGoodsListAdmin = name =>  {
  if (name == null || name == '') {
    return requests({ method: 'get', url: `gameGoods/admin`})
  }
  return requests({ method: 'get', url: `gameGoods/admin?name=${name}`})
};

//商品详情（管理员）
export const getGameGoodsDetailAdmin = (gameGoodsId) => requests({ method: 'get', url: `gameGoods/admin/${gameGoodsId}`})

//按照状态查询订单列表
export const getOrderListAdmin = (orderStatus) => {
  if (orderStatus == 4) {
    return requests({ method: 'get', url: `order/admin/list`})
  }
  return requests({ method: 'get', url: `order/admin/list?status=${orderStatus}`})
}

//用户查询（管理员）
export const findUser = (userId) => requests({ method: 'get', url: `user/${userId}`})

//查询日志（管理员）
export const getOrderLogAdmin = (orderSn) => {
  if (orderSn == null || orderSn == '') {
    return requests({ method: 'get', url: `order/admin/log` })
  }
  return requests({ method: 'get', url: `order/admin/log?orderSn=${orderSn}`})
}

//退款审核
export const checkRefund = (orderSubSn, refundStatus) => requests({ method: 'get', url: `order/checkRefund?orderSubSn=${orderSubSn}&refundStatus=${refundStatus}`})

//退款订单列表
export const refundOrderList = () => requests({ method: 'get', url: `order/checkRefundList`})

//-----------------------------------springboot API------------------------------------------------------
//获取个人信息
export const getUserInfo = () => requests({ method: 'get', url: `user`});

//注册
export const register = (data) => requests({ method: 'post', url: `user`, data});

//修改个人信息
export const updateUser = (data) => requests({ method: 'put', url: `user`, data})

//获取游戏资讯列表的数据
export const getGameNewsList = (year, month, name) => {
  //箭头函数可以在程序任意地方使用,箭头函数返回即为服务器的数据
  //下面箭头函数返回值：返回的是什么? promise,即为返回服务器的数据
  //return关键字，千万别忘记书写，如果忘记书写，你在任意地方获取的都是undeinfed
  if (year != null && year != '' && (name == null || name == '')) {
    return requests({ method: 'get', url: `/gameNews?year=${year}&month=${month}`});
  }
  if ((year == null || year == '') && name != null && name != '') {
    return requests({ method: 'get', url: `/gameNews?name=${name}`});
  }
  year = dateUtil.getYear()
  month = dateUtil.getMonth()
  return requests({ method: 'get', url: `/gameNews?year=${year}&month=${month}`});
}

//修改商品信息
export const updateGameGoods = (data) => requests({ method: 'put', url: `gameGoods`, data})

//添加商品
export const addGameGoods = (data) => requests({ method: 'post', url: `gameGoods`, data})

//获取游戏商品列表的数据
export const getGameGoodsList = name =>  {
  if (name == null || name == '') {
    return requests({ method: 'get', url: `gameGoods`})
  }
  return requests({ method: 'get', url: `gameGoods?name=${name}`})
};

//获取购物车信息
export const getCartInfo = () => requests({ method: 'get', url: `cart`})

//添加&修改购物车信息
export const updateCartInfo = (data) => requests({ method: 'post', url: `cart`, data})

//删除购物车信息
export const deleteCartInfo = (data) => requests({ method: 'delete', url: `cart`, data})

//创建订单
export const createOrder = (data) => requests({ method: 'post', url: `order`, data})

//订单详情
export const getOrderDetail = (orderSn) => requests({ method: 'get', url: `order/${orderSn}`})

//订单详情（管理员）
export const getOrderDetailAdmin = (orderSn) => requests({ method: 'get', url: `order/admin/${orderSn}`})

//-----------------------------------springcloud API------------------------------------------------------
// //获取个人信息
// export const getUserInfo = () => requests({ method: 'get', url: `user/getUserInfo`});

// //注册
// export const register = (data) => requests({ method: 'post', url: `user/register`, data});

// //修改个人信息
// export const updateUser = (data) => requests({ method: 'put', url: `user/update`, data})

// //获取游戏资讯列表的数据
// export const getGameNewsList = (year, month, name) => {
//   //箭头函数可以在程序任意地方使用,箭头函数返回即为服务器的数据
//   //下面箭头函数返回值：返回的是什么? promise,即为返回服务器的数据
//   //return关键字，千万别忘记书写，如果忘记书写，你在任意地方获取的都是undeinfed
//   if (year != null && year != '' && (name == null || name == '')) {
//     return requests({ method: 'get', url: `/gameNews/list?year=${year}&month=${month}`});
//   }
//   if ((year == null || year == '') && name != null && name != '') {
//     return requests({ method: 'get', url: `/gameNews/list?name=${name}`});
//   }
//   year = dateUtil.getYear()
//   month = dateUtil.getMonth()
//   return requests({ method: 'get', url: `/gameNews/list?year=${year}&month=${month}`});
// }

// //修改商品信息
// export const updateGameGoods = (data) => requests({ method: 'put', url: `gameGoods/update`, data})

// //添加商品
// export const addGameGoods = (data) => requests({ method: 'post', url: `gameGoods/add`, data})

// //获取游戏商品列表的数据
// export const getGameGoodsList = name =>  {
//   if (name == null || name == '') {
//     return requests({ method: 'get', url: `gameGoods/get`})
//   }
//   return requests({ method: 'get', url: `gameGoods/get?name=${name}`})
// };

// //获取购物车信息
// export const getCartInfo = () => requests({ method: 'get', url: `cart/get`})

// //添加&修改购物车信息
// export const updateCartInfo = (data) => requests({ method: 'post', url: `cart/add`, data})

// //删除购物车信息
// export const deleteCartInfo = (data) => requests({ method: 'delete', url: `cart/delete`, data})

// //创建订单
// export const createOrder = (data) => requests({ method: 'post', url: `order/create`, data})

// //订单详情
// export const getOrderDetail = (orderSn) => requests({ method: 'get', url: `order/detail?orderSn=${orderSn}`})

// //订单详情（管理员）
// export const getOrderDetailAdmin = (orderSn) => requests({ method: 'get', url: `order/admin/detail?orderSn=${orderSn}`})